/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin;

import org.digitalsoul.switchplugin.editoractions.MaintainSelectionEditorAction;
import org.digitalsoul.switchplugin.model.Configuration;
import org.digitalsoul.switchplugin.model.ISwitchFile;
import org.digitalsoul.switchplugin.model.ISwitchItem;
import org.digitalsoul.switchplugin.model.ItemRing;
import org.digitalsoul.switchplugin.model.SwitchFile;
import org.digitalsoul.switchplugin.prefs.Preferences;
import org.eclipse.core.resources.IFile;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

public class FileManager {

    protected final Finder finder;

    public FileManager() {
        finder = new Finder();
    }
    
    private static FileManager instance = new FileManager();
    private ItemRing ring = new ItemRing();
    
    public void switchFile() {
        IWorkbenchPage activePage = getActivePage();
        IEditorPart editor = activePage.getActiveEditor();
        if (editor != null) {
            IEditorInput input = editor.getEditorInput();
            if (input != null) {
                IFile currentFile = (input instanceof IFileEditorInput) ? ((IFileEditorInput) input).getFile() : null;
                if (currentFile != null) {
                	switchFile(currentFile);
                }
            }
        }
    }
    
    public void switchFile(IFile currentFile) {
        ItemRing items = loadItems();
        ISwitchFile nextFile = finder.findNextFile(new SwitchFile(currentFile), items);
        if (nextFile != null) {
            IFile foundFile = nextFile.getIFile();
            if (foundFile != null && foundFile.exists()) {
            	IEditorPart oldEditorPart = getActivePage().getActiveEditor();
            	IEditorPart newEditorPart = openFileInEditor(foundFile);
                if (newEditorPart != null) {
                	applyEditorActions(foundFile, currentFile, newEditorPart, oldEditorPart);	
                }
            }
        }
    }
    
    public IEditorPart openFileInEditor(IFile fileToOpen) {
        IWorkbenchPage activePage = getActivePage();
        IEditorPart newEditor = null;
        try {
           newEditor = IDE.openEditor(activePage, fileToOpen, true);
            
        }
        catch (PartInitException e) {
            e.printStackTrace();
        }
        return newEditor;
    }

	private IWorkbenchPage getActivePage() {
		IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		return activePage;
	}

	private void applyEditorActions(IFile newFile, IFile lastFile, IEditorPart newEditor, IEditorPart lastEditor) {
    	new MaintainSelectionEditorAction().applyAction(newFile, lastFile, newEditor, lastEditor);
	}
	
	protected ItemRing loadItems() {
        ring.clear();
        Configuration config = Preferences.load();
        if (config.hasSets() && config.getActiveSet() != null) { 
            for (ISwitchItem item : config.getActiveSet().getItems()) {
                ring.add(item);
            }
        }
        return ring;
    }
    
    public static FileManager getInstance() {
        return instance;
    }
}
